package com.xh.demo.common.handler;

import com.xh.demo.common.enums.ErrorEnum;
import com.xh.demo.common.exception.BusinessException;
import com.xh.demo.common.utils.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Name GlobalExceptionHandler
 * @Description 全局异常处理器
 * @Author wen
 * @Date 2019-08-01
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler extends CommonExceptionHandler{

    /**
     * @Name handleMaxUploadSizeExceededException
     * @Description 处理文件上传大小超出异常
     * @Author wen
     * @Date 2019/8/1
     * @return java.lang.Object
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Object handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex, HttpServletRequest request){
        log.error("BusinessException : [requestIP:{}, requestUrl:{}, errorMsg]",
                RequestUtil.getIpAddress(request), request.getRequestURI(), ex);
        return super.result(ErrorEnum.MAX_UPLOAD_SIZE_EXCEEDED, request);
    }

    /**
    * @Name handleRuntimeException
    * @Description 处理所有运行时异常
    * @Author wen
    * @Date 2019/8/1
    * @return java.lang.Object
    */
    @ExceptionHandler(RuntimeException.class)
    public Object handleRuntimeException(RuntimeException ex, HttpServletRequest request){
        log.error("BusinessException : [requestIP:{}, requestUrl:{}, errorMsg]",
                RequestUtil.getIpAddress(request), request.getRequestURI(), ex);
        return super.result(ErrorEnum.UNKNOWN_ERROR, request);
    }

    /**
     * @Name handleBusinessException
     * @Description 处理业务异常
     * @Author wen
     * @Date 2019/8/1
     * @return java.lang.Object
     */
    @ExceptionHandler(BusinessException.class)
    public Object handleBusinessException(BusinessException ex, HttpServletRequest request){
        log.error("BusinessException : [requestIP:{}, requestUrl:{}, errorCode:{}, errorMsg:{}]",
                RequestUtil.getIpAddress(request), request.getRequestURI(), ex.getCode(), ex.getMsg());
        return super.result(ex, request);
    }

    /**
    * @Name handleMethodArgumentNotValidException
    * @Description 处理参数校验异常
    * @Author wen
    * @Date 2019/8/2
    * @return java.lang.Object
    */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request){
        BindingResult bindingResult = ex.getBindingResult();
        List<ObjectError> errors = bindingResult.getAllErrors();
        StringBuffer sb = new StringBuffer();
        for (ObjectError error : errors) {
            sb.append(error.getDefaultMessage());
        }
        String errorMsg = sb.toString();
        log.error("BindException : [requestIP:{}, requestUrl:{}, errorMsg:{}]",
                RequestUtil.getIpAddress(request), request.getRequestURI(), errorMsg);
        return super.result(ErrorEnum.REQUEST_PARAMETERS_ERROR, errorMsg, request);
    }


}
